#include "modelwidget.h"
#include "ui_modelwidget.h"

ModelWidget::ModelWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ModelWidget)
{
    ui->setupUi(this);

    m_hLayout = new QHBoxLayout(ui->showFrame);
    m_hLayout->setMargin(0);
    m_hLayout->setSpacing(0);
    m_hLayout->setContentsMargins(0,0,0,0);

     m_loadModel = new LoadObj(parent);
     QString curDir = QGuiApplication::applicationDirPath();
//     m_loadModel->load3DModel(QString("file:%1").arg(curDir)+"/3dModel/airplane2/airplane2.obj");
     m_loadModel->load3DModel(QString("file:%1").arg(curDir)+"/3dModel/huojian.fbx");

     m_hLayout->addWidget(m_loadModel);
     ui->lightPoint->toggle();
     //ui->lightDirect->toggle();
     //ui->lightSpot->toggle();
     ui->moveRotate->toggle();
     //ui->moveSpan->toggle();

     connect(this,&ModelWidget::changeBackColor,m_loadModel,&LoadObj::slotChangeBackColor);
     connect(this,&ModelWidget::changeLightColor,m_loadModel,&LoadObj::slotChangeLightColor);
     connect(this,&ModelWidget::changeLightSource,m_loadModel,&LoadObj::slotChangeLightSource);
     connect(this,&ModelWidget::changeLightIntensity,m_loadModel,&LoadObj::slotChangeLightIntensity);
     connect(this,&ModelWidget::changeMoveFunc,m_loadModel,&LoadObj::slotChangeMoveFunc);

     connect(this,&ModelWidget::rotateX,m_loadModel,&LoadObj::slotRotateX);
     connect(this,&ModelWidget::rotateY,m_loadModel,&LoadObj::slotRotateY);
     connect(this,&ModelWidget::rotateZ,m_loadModel,&LoadObj::slotRotateZ);

     connect(this,&ModelWidget::lightRotateX,m_loadModel,&LoadObj::slotLightRotateX);
     connect(this,&ModelWidget::lightRotateY,m_loadModel,&LoadObj::slotLightRotateY);
     connect(this,&ModelWidget::lightRotateZ,m_loadModel,&LoadObj::slotLightRotateZ);

     connect(m_loadModel,&LoadObj::sigTransPos,this,&ModelWidget::slotShowPos);
}

ModelWidget::~ModelWidget()
{
    delete ui;
    //delete m_loadModel;
    //delete m_hLayout;
}

void ModelWidget::on_colorBack_clicked()
{
    QColor color = QColorDialog::getColor(Qt::gray,this,tr("颜色对话框"));
    if(!color.isValid())
    {
        qDebug()<<"no select color";
        color = Qt::gray;
    }
    qDebug()<<"background color:"<<color;
    emit changeBackColor(color);
}

void ModelWidget::on_colorLight_clicked()
{
    QColor color = QColorDialog::getColor(Qt::white,this,tr("颜色对话框"));
    if(!color.isValid())
    {
        qDebug()<<"no select color";
        color = Qt::white;
    }
    qDebug()<<"light source color:"<<color;
    emit changeLightColor(color);
}

void ModelWidget::on_lightPoint_toggled(bool checked)
{
    if(checked)
        emit changeLightSource(1);
}

void ModelWidget::on_lightDirect_toggled(bool checked)
{
    if(checked)
        emit changeLightSource(2);
}

void ModelWidget::on_lightSpot_toggled(bool checked)
{
    if(checked)
        emit changeLightSource(3);
}

void ModelWidget::on_sliderIntensity_valueChanged(int value)
{
    qDebug()<<"light intensity: "<<value;
    emit changeLightIntensity(value);
}

void ModelWidget::on_sliderX_valueChanged(int value)
{
    emit rotateX(value);
    qDebug()<<"X:"<<value;
}

void ModelWidget::on_sliderY_valueChanged(int value)
{
    emit rotateY(value);
}

void ModelWidget::on_sliderZ_valueChanged(int value)
{
    emit rotateZ(value);
}

void ModelWidget::slotShowPos(int xpos,int ypos,int YAngle)
{
    QString str = "pos:("+QString::number(xpos,10)+","+QString::number(ypos,10)+"), Yangle:"+QString::number(YAngle,10);
    ui->curPos->setText(str);
}

void ModelWidget::on_moveRotate_toggled(bool checked)
{
    if(checked)
        emit changeMoveFunc(1);
}

void ModelWidget::on_moveSpan_toggled(bool checked)
{
    if(checked)
        emit changeMoveFunc(2);
}

void ModelWidget::on_lightSliderX_valueChanged(int value)
{
    emit lightRotateX(value);
}

void ModelWidget::on_lightSliderY_valueChanged(int value)
{
    emit lightRotateY(value);
}

void ModelWidget::on_lightSliderZ_valueChanged(int value)
{
    emit lightRotateZ(value);
}
